<?php
defined('BY_JYA') or exit('error');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($_RQ['id']){
		$params['group_id']=intval($_RQ['id']);
		$return['params']['id']=intval($_RQ['id']);
	}
	if($_RQ['title']){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$orderby=array();
	$pindex = max(1, intval($_RQ['page']));
	if($_RQ['num']){
		$psize = $_RQ['num'];
	}else{
		$psize = 10;
	}
	$total='';
	$list=pdo_getslice('jy_wenjstar_tiku',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['desc'] = $item['desc'] ? $item['desc'] : cfc('tools')->substr($item['content'],0,36);
		}
	}
	$grouptitle=pdo_getcolumn('jy_wenjstar_group',array('id'=>$this->session('group_id'),'uniacid'=>$_SESSION['uniacid']),'title');
	$return['list']=$list?$list:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'export'=>'export',
		'tabs'=>array(
			array('op'=>'list','title'=>'题目列表'),
			array('op'=>'edit','title'=>'添加题目'),
		),
		'list'=>array('data'=>'list','tip'=>'[温馨提示]：排序越大，越靠前！','list'=>array(
			array('name'=>'paixu','title'=>'排序','type'=>'string'),
			array('type'=>'select','name'=>'group_id','options'=>'groups','title'=>'所属题库'),
			array('name'=>'title','title'=>'题目标题','type'=>'string'),
			array('name'=>'type','title'=>'题目类型','type'=>'radio','options'=>'types'),
			array('name'=>'status','title'=>'状态','type'=>'switch','set'=>'set'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
		)),
		'search'=>array(
			array('name'=>'title','width'=>10,'title'=>'题目标题','type'=>'string'),
		),
	);
	$return['groups']=pdo_getall('jy_wenjstar_group',array('uniacid'=>$_SESSION['uniacid']),array('title','id'),'id');
	$return['types']=$this->ti_types;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='set'){
	$post=get_postdata();
	$id=intval($_RQ['id']);
	pdo_update('jy_wenjstar_tiku',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	exi('操作成功！');
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_wenjstar_tiku',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_wenjstar_tiku',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			}
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_wenjstar_tiku',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$item=array('status'=>1,'type'=>4);
	}
	$item['xuanxiang']=json_decode($item['xuanxiang'],true);
	$item['images']=json_decode($item['images'],true);
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回题目列表'),
		),
		'edit'=>array(
			array('name'=>'paixu','title'=>'排序','type'=>'string'),
			array('name'=>'title','title'=>'题目标题','type'=>'string','must'=>true),
			array('name'=>'type','title'=>'题目类型','type'=>'radio','options'=>'types'),
			array('type'=>'select','name'=>'group_id','options'=>'groups','title'=>'所属题库'),
			array(
				'type'=>'array_simple',
				'show'=>array('type'=>array(4,5)),
				'name'=>'xuanxiang',
				'title'=>'题目选项',
				'add_tip'=>'添加题目选项',
				'fields'=>array(
					array('type'=>'xuhao','name'=>'danan_key','title'=>'序号','help'=>''),
					array('type'=>'string','name'=>'title','title'=>'选项名称','help'=>''),
					array('type'=>'switch','name'=>'needtian','title'=>'需要补充填空？','help'=>''),
				),
			),
			array('name'=>'status','title'=>'状态','type'=>'switch'),
		)
	);
	$return['groups']=pdo_getall('jy_wenjstar_group',array('uniacid'=>$_SESSION['uniacid']),array('title','id'),'id');
	$return['types']=$this->ti_types;
	$return['danan_key']=$this->danan_key;
	$return['duicuo']=$this->duicuo;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$fields=array('title','type','group_id','desc','score','images','xuanxiang','daan','datiid','paixu','status');
	$data=array_fields($fields,$postdata);
	$data['images']=ijson_encode($data['images']);
	$data['xuanxiang']=ijson_encode($data['xuanxiang']);
	if(empty($data['title'])){
		exi('请填写题目标题！','error');
	}
	if(empty($data['xuanxiang'])){
		exi('请填写题目选项！','error');
	}
	if(empty($data['group_id'])){
		exi('请选择所属题库！','error');
	}
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_wenjstar_tiku',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		exi('编辑题目成功！','','goback');
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=TIMESTAMP;
		pdo_insert('jy_wenjstar_tiku',$data);
		$id=pdo_insertid();
		exi('添加题目成功！','','goback');
	}
}